﻿using System;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class FormulaOperandAttr : TFormulaOperand<LFloat>
    {
        private EAttributeType m_attrType;
        private AttributeProperty m_Attr;
        private EOperatorType m_OperatorType;
        public override EFormulaOperandType opType => EFormulaOperandType.Attr;

        public override LFloat GetValue(IFormulaObject rSelf, IFormulaObject rTarget)
        {
            if (this.m_Attr == null)
            {
                return LFloat.zero;
            }

            switch (this.m_OperatorType)
            {
                case EOperatorType.Attribute:
                    return this.m_Attr.Get(this.m_attrType);
                case EOperatorType.Lv:
                    return new LFloat(this.m_Attr.lv);
                
            }

            return LFloat.zero;
        }

        protected override void Set(object param)
        {
            if (param == null)
            {
                return;
            }

            switch (param)
            {
                case AttributeProperty rAttr:
                {
                    this.m_Attr = rAttr;
                    break;
                }
                case string rParam:
                {
                    if (rParam == "lv")
                    {
                        this.m_OperatorType = EOperatorType.Lv;
                    }
                    else if (Enum.TryParse(rParam, true, out this.m_attrType))
                    {
                        this.m_OperatorType = EOperatorType.Attribute;
                    }
                    else
                    {
                        this.m_OperatorType = EOperatorType.None; 
                    }
                    break;
                }
            }
        }


        public override string ToString()
        {
            return $"{this.m_attrType}: {this.m_Attr?.Get(this.m_attrType) ?? 0}";
        }

        public override void Reset()
        {
            base.Reset();
            this.m_attrType = EAttributeType.Min;
            this.m_Attr = null;
        }

        enum EOperatorType
        {
            None,
            Attribute,
            Lv,
        }
    }
}